package com.znpc.entity;

import com.znpc.valmap.ValMap;
import lombok.Data;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Objects;

/**
 * @Date 2021-09-05 16:10
 * @Author YangFan
 * @Description 订单数据类
 */
public class Order {

    public String orderId;
    public String userId;
    public String batchId;
    public LocalDate arriveDate;
    public LocalDate deliverDate;
    public Integer number;
    public String pName;
    public String color;  //a
    public String bm;  //基料
    public String fm;  //填料
    public Integer fmRation;  //填料比例
    public Integer halogen;

    public Order() {
    }

    public Order(XSSFRow row) {
        this.orderId = row.getCell(0).toString();
        this.userId = row.getCell(1).toString();
        this.batchId = row.getCell(3).toString();
        this.arriveDate = getDate(row.getCell(4));
        this.deliverDate = getDate(row.getCell(7));
        this.number = (int) Double.parseDouble(row.getCell(6).toString());
        this.pName = row.getCell(2).toString();
        this.color = row.getCell(5).toString();
        this.bm = row.getCell(8).toString();
        this.fm = row.getCell(10).toString();
        this.fmRation = (int) Double.parseDouble(row.getCell(11).toString());
        this.halogen = (int) Double.parseDouble(row.getCell(9).toString());
    }

    private LocalDate getDate(XSSFCell cell) {
        /*
         * java 中 POI 对 Excel 日期的解析结果成 01-八月-2021
         * 使用 cell.setCellType(CellType.STRING) 设置单元格类型后获取的是距离1900-01-01的天数
         * 需要进行转换
         */
        if (cell == null || cell.toString().isEmpty()) {
            return ValMap.BEGIN_DATE;
        }

        LocalDate pDate = LocalDate.of(1900, 1, 1);

        cell.setCellType(CellType.STRING);

        return pDate.plusDays((int) Double.parseDouble(cell.toString()) - 2);
    }

    @Override
    public String toString() {
        return "Order{" +
                "orderId='" + orderId + '\'' +
                ", userId='" + userId + '\'' +
                ", arriveDate=" + arriveDate +
                ", deliverDate=" + deliverDate +
                ", number=" + number +
                ", pName='" + pName + '\'' +
                ", color='" + color + '\'' +
                ", bm='" + bm + '\'' +
                ", fm='" + fm + '\'' +
                ", fmRation=" + fmRation +
                '}';
    }

}
